package com.autoreassociate.analysis;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import android.content.Context;
import android.widget.SimpleAdapter;
import android.widget.Toast;

import com.autoreassociate.R;

public class MessageLogManager {

	
	public static final String LOG_ICON_VIEW = "iconView";
	
	public static final String LOG_DATE_VIEW = "dateView";
	
	public static final String LOG_MSG_VIEW = "logMsgView";
	
	
	protected static SimpleDateFormat dataFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
	
	
	//protected ListView logView;
	protected SimpleAdapter adapter;
	
	protected Context context;
	
	protected List<Map<String, ?>> logMsgList = new ArrayList<Map<String, ?>>();;
	
	protected boolean toastViewToastMessage;
	
	




	//public MessageLogManager(Context context, ListView logView, boolean toastViewToastMessage) {
	public MessageLogManager(Context context, boolean toastViewToastMessage) {

		this.context = context;
		//this.logView = logView;
		//this.logView = new ListView(context);
		this.toastViewToastMessage = toastViewToastMessage;
		
        adapter = new SimpleAdapter(context,
								                  logMsgList,
								                  R.layout.log_list_layout,
								                  new String[]{LOG_ICON_VIEW, LOG_DATE_VIEW, LOG_MSG_VIEW},
								                  new int[]{R.id.iconView, R.id.dateView, R.id.logMsgView}) {
				//@Override
				//public View getView(
				//int position,
				//View convertView,
				//ViewGroup parent) {
				//// TODO Auto-generated method stub
				//
				//convertView.findViewById(R.id.iconView).
				//return super.getView(position, convertView, parent);
				//}
		};

		//logView.setAdapter(adapter);
		
		
	}
	
	
	
	public void printV(int resId) {
		if (context != null) {
			printV(context.getText(resId).toString());
		}

	}
	
	public void printV(int resId, int iconId) {
		if (context != null) {
			printV(context.getText(resId).toString(), iconId);
		}

	}
	
	
	public void printV(String msg) {

		printV(msg, R.drawable.state_wait);
	}
	
	public void printV(String msg, int iconId) {
		
		if (logMsgList != null && adapter != null) {
			
			Map<String, Object> newLog = new HashMap<String, Object>();
			newLog.put(LOG_ICON_VIEW, iconId);
			newLog.put(LOG_DATE_VIEW, dataFormat.format(System.currentTimeMillis()));
			newLog.put(LOG_MSG_VIEW, msg);
			
//			if (iconId == R.drawable.state_ng) {
//				newLog.put(LOG_BACKGROUND, "#FFFF0001");
//			}
			
			logMsgList.add(newLog);
			adapter.notifyDataSetChanged();
			//logView.scrollTo(0,0);

			//logView.append(String.format(SCREEN_LOG_FORMAT, System.currentTimeMillis(), msg));
		}
		
	}
	

	public void printT(int resId) {
		if (context != null
				&& toastViewToastMessage) {
			Toast.makeText(context, resId, Toast.LENGTH_SHORT).show();
		}
	
	}
	
	public void printT(String msg) {
		if (context != null
				&& toastViewToastMessage) {
			Toast.makeText(context, msg, Toast.LENGTH_SHORT).show();
		}
	}
	
	public void printForceT(String msg) {
		if (context != null) {
			Toast.makeText(context, msg, Toast.LENGTH_SHORT).show();
		}
	}
	
	
	public void printVT(int resId, int iconId) {
		printV(resId, iconId);
		printT(resId);
	}
	
	public void printVT(int resId) {
		printV(resId);
		printT(resId);
	}
	
	public void printVT(String mag, int iconId) {
		printV(mag, iconId);
		printT(mag);
	}
	
	public void printVT(String mag) {
		printV(mag);
		printT(mag);
	}
	
	public void printVT(String format, int iconId, Object... args) {
		printVT(String.format(format, args), iconId);
		
	}
	
	public void printVT(String format, Object... args) {
		printVT(String.format(format, args));
		
	}
	
	public void printE(Exception e) {

		e.printStackTrace();
	}
	
	
	
	
	public boolean isToastViewToastMessage() {
		return toastViewToastMessage;
	}



	public void setToastViewToastMessage(boolean toastViewToastMessage) {
		this.toastViewToastMessage = toastViewToastMessage;
	}
	
	
	
	public void clearViewListLog() {
		if (adapter == null) {
			return;
		}
		
    	logMsgList.clear();
    	adapter.notifyDataSetChanged();
	}
	
	
	public SimpleAdapter getAdapter() {
		return this.adapter;
	}
	
	//------------------------------------------------------------------------

}
